// https://leetcode.cn/problems/find-words-that-can-be-formed-by-characters/

/**
 * @param {string[]} words
 * @param {string} chars
 * @return {number}
 */
export const countCharacters = function (words: string[], chars: string) {
  let result = 0;
  let letterCounter = { pass: true };
  for (let c of chars) {
    letterCounter[c] = letterCounter[c] ? letterCounter[c] + 1 : 1;
  }
  for (let w of words) {
    let cDic = Object.create(letterCounter);
    for (let c of w) {
      if (cDic[c]) {
        cDic[c]--;
      } else {
        cDic.pass = false;
        break;
      }
    }
    if (cDic.pass) {
      result += w.length;
    }
  }
  return result;
};
